package cn.wjssl.test.domain;

import cn.wjssl.domain.strategy.model.entity.RaffleAwardEntity;
import cn.wjssl.domain.strategy.model.entity.RaffleFactorEntity;
import cn.wjssl.domain.strategy.service.IStrategyService;
import cn.wjssl.domain.strategy.service.armory.IStrategyArmory;
import cn.wjssl.domain.strategy.service.rule.chain.impl.RuleWeightLogicChain;
import cn.wjssl.domain.strategy.service.rule.tree.impl.RuleLockLogicTreeNode;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.util.ReflectionTestUtils;

import javax.annotation.Resource;

@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class RaffleTest {

    @Resource
    private IStrategyService raffleStrategy;

    @Resource
    private RuleWeightLogicChain ruleWeightLogicChain;

    @Resource
    private IStrategyArmory strategyArmory;

    @Resource
    private RuleLockLogicTreeNode ruleLockLogicTreeNode;

    @Test
    public void raffleBefore() {
        // 装配奖池
        boolean isScuccess = strategyArmory.assembleLotteryStrategy(100001L);
        log.info("测试结果：{}", isScuccess);
        // 默认积分是4500L, 可以通过反射修改
        // ReflectionTestUtils.setField(ruleWeightLogicFilter, "userScore", 40500L);
        ReflectionTestUtils.setField(ruleWeightLogicChain, "userScore", 40500L);

        // 普通用户
        RaffleFactorEntity raffleFactorEntity = RaffleFactorEntity.builder()
                 .userId("lxd")
                 .strategyId(100001L)
                 .build();

        // 黑名单用户
        //RaffleFactorEntity raffleFactorEntity = RaffleFactorEntity.builder()
        //         .userId("user003")
        //         .strategyId(100001L)
        //         .build();
        for (int i = 0; i < 10; i++) {
            RaffleAwardEntity raffleAwardEntity = raffleStrategy.performRaffle(raffleFactorEntity);
            // log.info("请求参数：{}", JSON.toJSONString(raffleFactorEntity));
            // log.info("测试结果：{}", JSON.toJSONString(raffleAwardEntity));
        }
    }

    @Test
    public void raffleCenter() {
        // 装配奖池
        boolean isScuccess = strategyArmory.assembleLotteryStrategy(100003L);
        log.info("测试结果：{}", isScuccess);

        RaffleFactorEntity raffleFactorEntity = RaffleFactorEntity.builder()
                .userId("lxd")
                .strategyId(100003L)
                .build();

        RaffleAwardEntity raffleAwardEntity = raffleStrategy.performRaffle(raffleFactorEntity);
        log.info("请求参数：{}", JSON.toJSONString(raffleFactorEntity));
        log.info("测试结果：{}", JSON.toJSONString(raffleAwardEntity));
    }

    @Test
    public void raffle() throws InterruptedException {
        // 装配奖池
        boolean isScuccess = strategyArmory.assembleLotteryStrategy(100006L);
        log.info("测试结果：{}", isScuccess);

        // 通过反射配置抽奖次数, 抽奖积分
        ReflectionTestUtils.setField(ruleWeightLogicChain, "userScore", 2000L);
        ReflectionTestUtils.setField(ruleLockLogicTreeNode, "userRaffleCount", 1L);

        RaffleFactorEntity raffleFactorEntity = RaffleFactorEntity.builder()
                .userId("lxd")
                .strategyId(100006L)
                .build();
        while (true) {
            RaffleAwardEntity raffleAwardEntity = raffleStrategy.performRaffle(raffleFactorEntity);
            log.info("测试结果：{}", JSON.toJSONString(raffleAwardEntity));

            Thread.sleep(3000);
        }
    }

}
